// @File:           RedisAuthPlugin.cpp
// @ProjectName:    mosquitto_auth_plugin
// @Description:    ${description}
// @Author:         wujehy
// @Email:          wujehy@qq.com
// @Time:           2021-03-16 14:43

#include "RedisAuthPlugin.h"
#include "Logger.h"
#include "RedisTool.h"
#include "mosquitto_plugin.h"

void RedisAuthPlugin::init() {
    if (redisTool == nullptr) {
        redisTool = new RedisTool;
        int initStatus = redisTool->init();
        if (initStatus == 0) {
            gDebug << "初始化redis 成功";
        } else {
            gError << "初始化redis 失败";
            exit(initStatus);
        }
    }
}

int RedisAuthPlugin::checkUser(const std::string &username, const std::string &password) {
    gDebug << "RedisAuthPlugin::checkUser username = " << username << " password =" << password;
    return AuthPluginInterface::PLUGIN_SUCCESS;
}

int RedisAuthPlugin::getPskKey(mosquitto *client, const char *hint, const char *identity, char *key, int max_key_len) {
    gDebug << " RedisAuthPlugin::getPskKey , hint = " << hint << " ,identity = " << identity << " key = " << " max_key_len = " << max_key_len;
    return PLUGIN_DEFER;
}

int RedisAuthPlugin::checkAcl(mosquitto *client, AuthPluginInterface::ACLTypeEnum access, const mosquitto_acl_msg *msg) {
    gDebug << " RedisAuthPlugin::checkAcl , access = " << access << " ,qos = " << msg->qos << " topic = " << msg->topic;

    return PLUGIN_SUCCESS;
}

int RedisAuthPlugin::cleanupSecurity(mosquitto_opt *auth_opts, int auth_opts_count, bool reload) {
    gDebug << " RedisAuthPlugin::cleanupSecurity : reload =" << reload;

    return 0;
}

int RedisAuthPlugin::initSecurity(mosquitto_opt *auth_opts, int auth_opts_count, bool reload) {
    gDebug << " RedisAuthPlugin::initSecurity : reload =" << reload;
    return 0;
}

RedisAuthPlugin::~RedisAuthPlugin() {
    if (redisTool != nullptr) {
        delete redisTool;
    }
}
